package com.zhongketech.sunflower.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.adapter.ContactAdapter;
import com.zhongketech.sunflower.db.InviteMessgeDao;
import com.zhongketech.sunflower.db.UserDao;
import com.zhongketech.sunflower.domain.User;
import com.zhongketech.sunflower.secondfragment.BaseFragment;

/**
 * 联系人列表页
 *
 */
@SuppressLint("NewApi")
public class Copy_2_of_ContactlistFragment extends Fragment {

	private ViewPager mViewPager;
	private ViewPagerAdapter mAdapter;
	private ActionBar mTabBar;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_contact_list_2, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initView();
	}
	
	
	private void initView()
	{
	    mViewPager = (ViewPager)getView(). findViewById(R.id.viewpager);
        mAdapter = new ViewPagerAdapter(getActivity(), mViewPager);
        mTabBar = getActivity().getActionBar();

        mAdapter.addTab(mTabBar.newTab().setIcon(R.drawable.actionbar_camera_icon),
                BaseFragment.class, null);
        mAdapter.addTab(mTabBar.newTab().setIcon(R.drawable.actionbar_camera_icon),
                BaseFragment.class, null);
        mAdapter.addTab(mTabBar.newTab().setIcon(R.drawable.actionbar_camera_icon),
                BaseFragment.class, null);
	}
	
	
	private static class ViewPagerAdapter extends FragmentStatePagerAdapter implements TabListener, OnPageChangeListener {

		private Context mContext;
		private ActionBar mActionBar = null;
		private ViewPager mViewPager = null;
		private ArrayList<TabInfo> mTabList = new ArrayList<TabInfo>();

		public ViewPagerAdapter(Activity activity, ViewPager viewPager) {
			// TODO Auto-generated constructor stub
			super(((FragmentActivity) activity).getSupportFragmentManager());
			
			mContext = activity;
			mActionBar = activity.getActionBar();
			
//			Log.e("AAA","activity:"+activity);
//			Log.e("AAA","mActionBar:"+mActionBar);
//			Log.ERROR("AAA","activity:"+activity);
			/*
			 * mActionBar.setCustomView(getCustomView(), new LayoutParams(
			 * LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			 */
//			mActionBar.setDisplayOptions(mActionBar.getDisplayOptions()
//					^ ActionBar.DISPLAY_SHOW_HOME
//					^ ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_HOME
//					| ActionBar.DISPLAY_SHOW_TITLE);
//			mActionBar.setDisplayShowHomeEnabled(false);
//			mActionBar.
			mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
			mViewPager = viewPager;
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
		}

		/** Override OnPageChangeListener start **/
		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int position) {
			// TODO Auto-generated method stub
			mActionBar.setSelectedNavigationItem(position);
		}

		/** Override OnPageChangeListener end **/

		/** Override TabListener start **/
		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			// TODO Auto-generated method stub
			Object tag = tab.getTag();
			for (int i = 0; i < mTabList.size(); i++) {
				if (mTabList.get(i) == tag) {
					mViewPager.setCurrentItem(i);
				}
			}
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// TODO Auto-generated method stub

		}

		/** Override TabListener end **/

		/** Override FragmentPagerAdapter start **/
		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			TabInfo tab = mTabList.get(position);
			if (tab.fragment == null) {
				tab.fragment = Fragment.instantiate(mContext,
						tab.clazz.getName(), tab.bundle);
			}
			return tab.fragment;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mTabList.size();
		}

		/** Override FragmentPagerAdapter end **/

		private View getCustomView() {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			return inflater.inflate(R.layout.fcl_title_panel_layout, null);
		}

		public void addTab(Tab tab, Class<?> clazz, Bundle bundle) {
			TabInfo tabInfo = new TabInfo(clazz, bundle);
			tab.setTag(tabInfo);
			tab.setTabListener(this);
			mTabList.add(tabInfo);
			mActionBar.addTab(tab);
			notifyDataSetChanged();
		}

		private static final class TabInfo {
			private final Class<?> clazz;
			private final Bundle bundle;
			Fragment fragment;

			TabInfo(Class<?> clazz, Bundle bundle) {
				this.clazz = clazz;
				this.bundle = bundle;
			}
		}

	}

}
